var PATH_PREFIX ="";

function setPathPrefix(path){
	PATH_PREFIX =  path;
}

var relData = "";

$(function() {
	// 给select2组件加载样式
	WASP_WIDGET.initializeSelectStyle();
	$(".use-select2").select2();
	// 注册清空事件
    WASP_WIDGET.registerResetClearEvent();
    
    $("#assetUnitId_inp").change(function(){
    	
    	var actionUrl= PATH_PREFIX + "queryAsseAndTradeUnitRelById.do";
		$.ajax({
			type: 'POST',
			url: actionUrl,
			dataType:'json',
			data:{
				'assetUnitId' : $("#assetUnitId_inp").val()
			},
			success: function(data){
				if(null != data && data.length > 0){
					relData = new Array();
					for (var i = 0; i < data.length; i++) {
						relData[i] = data[i].tradeUnitId;
					}
					relData = relData.join(",");
				}else{
					relData = "";
				}
				queryByCondtion(true);
			}
		});
    })
    $("#assetUnitId_inp").change();
});

var $tradeUnitList = $('#tradeUnitList');

$(function() {
	$tradeUnitList.jqGrid({   
		url:PATH_PREFIX+'queryTradeUnitList.do',    
		//caption:'交易单元列表<button type="button" class="btn btn-default btn-sm btn-small btn-fullscreen" title="" tabindex="-1"><i class="fa fa-arrows-alt icon-fullscreen"></i></button>',  
		datatype: "json",
		colNames:["主键","交易代码","交易单元名称","交易单元类型","股东代码卡","管理人"],        
		colModel:[  
				{ name: 'tradeUnitId', index: 'tradeUnitId', hidden: true, key: true, resizable: true, sortable: false },       
		        { name: 'tradeCode', index: 'tradeCode',hidden: true, resizable: true, sortable: false },
		        { name: 'tradeName', index: 'tradeName', resizable: true, sortable: false },
		        { name: 'tradeType', index: 'tradeType', resizable: true, sortable: false ,
	            	formatter:function(cellvalue, options, rowObject){
	            		if(cellvalue =="01"){
	            			return "场内";
	            		}else if(cellvalue =="02"){
	            			return "场外";
	            		}else{
	            			return cellvalue;
	            		}
	            	}
	            },
		        { name: 'partnerCodeCard', index: 'partnerCodeCard', resizable: true, sortable: false },
		        { name: 'managementPeople', index: 'managementPeople', resizable: true, sortable: false }
				 ],
		rowNum: 9999,
        rowList: [20, 30, 50],
        rownumbers: true,
        rownumWidth: 50,
        prmNames: { search: "search", page: "pageNo", rows: "limit" },
        height: $(window).height()-320,
        width: false,
        autowidth:true,
        editurl: '',
        viewrecords: true,
        cellEdit: false,
        shrinkToFit: true,
        grouping: false,
        jsonReader: {
            root: "items", //结果集
            records: "total", //总记录数 
            total: "pageCount", //总页数
            page: "pageNo", //当前页 
            repeatitems: false // (4) 
        },  
		multiselect:true,
		//pager: "#tradeUnitListPage",						
		gridComplete: function(){
			var ids = $tradeUnitList.jqGrid('getDataIDs');
            for (var i = 0; i < ids.length; i++) {
        	   var id = ids[i];
        	   var curChk = $("#"+id+"").find(":checkbox");
			   if(relData.indexOf(id) != -1){
				   curChk.attr('checked', 'true');
			   }
            }
		}
	});
	$tradeUnitList.navGrid('#tradeUnitListage',{edit:false,add:false,del:false,search:false,refreshstate:'current'});
	$tradeUnitList.jqGrid('setFrozenColumns');
	jqGridResize($tradeUnitList);
});

/**
 * 查询按钮
 * 根据条件查询数据
 * @param flag
 */
function queryByCondtion(flag){
    var postData = $tradeUnitList.jqGrid("getGridParam", "postData");
    if (flag) {
    	$tradeUnitList.jqGrid("setGridParam",{"datatype": "json"}).trigger("reloadGrid",[{page:1}]);//重新载入Grid表格
    } else {
    	$tradeUnitList.jqGrid("setGridParam",{"datatype": "json"}).trigger("reloadGrid");//重新载入Grid表格
    }
}

function doSubmit(){
	$("#assetUnitId").val($("#assetUnitId_inp").val());
	var selectData = new Array();
	//获取表格所有数据
	var ids = $tradeUnitList.getRowData();
	for (var i = 0; i < ids.length; i++) {
		var curChk = $("#"+ids[i].tradeUnitId+"").find(":checkbox").is(":checked");
		if(curChk){
			selectData.push(ids[i].tradeUnitId);
		}
	} 
	
	if (null != selectData && selectData.length > 0) {
		var tradeUnitIds =  selectData.join(",");
		$("#tradeUnitId").val(tradeUnitIds);
	}else{
		toastr.warning('', '请选择交易单元数据！');
	}
	//提交表单
	SubmitAndPreventSecond("asseAndtradeUnitRelAddForm",true);
}
